# -*- coding: utf-8 -*-
import xlsxwriter
from FileAndTable_Name import filename, common_header
from get_AllData import sheet_name, temp


def set_common_header(sheetName, common_header):
    # 设置行高
    sheetName.set_row(0, 45)
    sheetName.set_row(1, 25)
    sheetName.set_row(2, 25)
    # 设置列宽
    sheetName.set_column("A:A", 5.75)
    sheetName.set_column("B:B", 38.75)
    sheetName.set_column("C:E", 5.25)
    sheetName.set_column("F:F", 6.13)
    sheetName.set_column("G:G", 7.75)

    # 标题
    head_format = f.add_format({
        'font_name': '宋体',
        'font_size': 22,
        'bold': True,
        'align': 'center',  # 水平居中
        'valign': 'vcenter',  # 垂直居中
    })
    # 第二行
    second_format = f.add_format({
        'font_name': '宋体',
        'font_size': 14,
        'border': 1,
        'align': 'center',  # 水平居中
        'valign': 'vcenter',  # 垂直居中
    })
    # 第二行小字
    sm_format = f.add_format({
        'font_name': '宋体',
        'font_size': 11,
        'border': 1,
        'align': 'center',  # 水平居中
        'valign': 'vcenter',  # 垂直居中
    })

    r = 0
    for i in common_header:
        for j in i.values():
            for k in j:
                c = 0
                for l in k:
                    if l == "人事档案目录":
                        sheetName.merge_range("A1:G1", "干部档案目录", head_format)
                    elif l == "序号":
                        sheetName.merge_range("A2:A3", "类\n号", second_format)
                    elif l == "材料名称":
                        sheetName.merge_range("B2:B3", l, second_format)
                    elif l == "材料形成时间":
                        sheetName.merge_range("C2:E2", l, sm_format)
                    elif l == "页数":
                        sheetName.merge_range("F2:F3", "页\n数", second_format)
                    elif l == "备注":
                        sheetName.merge_range("G2:G3", l, second_format)
                    elif l in "年月日":
                        sheetName.write(r, c, l, sm_format)
                    c += 1
                r += 1


def set_sheet_head(i, sheetName, sheet_head):
    # 小标题序号
    titlerank_format = f.add_format({
        'font_name': '宋体',
        'font_size': 12,
        'bold': True,
        'border': 1,
        'align': 'center',  # 水平居中
        'valign': 'vcenter',  # 垂直居中
    })
    # 小标题
    title_format = f.add_format({
        'font_name': '宋体',
        'font_size': 12,
        'bold': True,
        'border': 1,
        'align': 'left',  # 水平居中
        'valign': 'vcenter',  # 垂直居中
    })
    #
    data_format = f.add_format({
        'font_name': '宋体',
        'font_size': 12,
        'border': 1,
        'align': 'center',  # 水平居中
        'valign': 'vcenter',  # 垂直居中
        'text_wrap': True
    })

    r = 3
    for j in sheet_head:
        c = 0
        for k in j:
            sheetName.set_row(r, 30)
            if r == 3:
                sheetName.write(r, c, k, titlerank_format)
            elif r == 4 and "-" in i:
                sheetName.write(r, c, k, titlerank_format)
            elif c == 5:
                sheetName.write(r, c, k, titlerank_format)
            else:
                sheetName.write(r, c, k, data_format)
            c += 1
        r += 1


def set_sheet_data(sheetName, sheet_data):
    # B列
    title_format = f.add_format({
        'font_name': '宋体',
        'font_size': 12,
        'border': 1,
        'align': 'left',  # 水平居中
        'valign': 'vcenter',  # 垂直居中
    })
    # B列
    data_format = f.add_format({
        'font_name': '宋体',
        'font_size': 12,
        'border': 1,
        'align': 'center',  # 水平居中
        'valign': 'vcenter',  # 垂直居中
    })

    r = 5
    for j in sheet_data:
        c = 0
        for k in j:
            sheetName.set_row(r, 30)
            if c == 1:
                sheetName.write(r, c, k, title_format)
            else:
                sheetName.write(r, c, k, data_format)
            c += 1
        r += 1


if __name__ == "__main__":
    f = xlsxwriter.Workbook("/home/cedar/桌面/" + filename + ".xlsx")

    SH_NUM = 0  # 控制每个sheet表的小标题
    SD_NUM = 0  # 控制每个
    for i in sheet_name:
        sheetName = f.add_worksheet(i)

        set_common_header(sheetName, common_header)

        set_sheet_head(i, sheetName, temp[SH_NUM])
        SH_NUM += 1

    f.close()
